import{Mongoose,Schema,Model,Document} from 'mongoose';
import { BaseDao} from './BaseDao';
import{ User} from '../models/User';
import{ Doc} from '../models/Doc';
export class DocsDao extends BaseDao
{
  docModel:Model<any>;
  constructor(){
    super();
    this.docModel = this.dbManager.getModel("mDoc");
  }

  add(doc:Doc,callback:Function)
  {
    var doc_ = new this.docModel(doc);
    doc_.save(callback);
  }

  get(docId:string,callback:Function)
  {
    this.docModel.findOne({_id:docId}).populate("project").populate("group").exec(callback);
  }

  getsByProjectId(id:string,callback:Function)
  {
    this.docModel.find({project:id}).populate("project").populate("group").exec(callback);
  }

  getsByGroudId(id:string,callback:Function)
  {
    this.docModel.find({group:id}).exec(callback);
  }

  updateDoc(doc:Doc,callback:Function)
  {
    var query = {_id:doc._id};
    var updater = {
      name:doc.name,
      action:doc.action,
      description:doc.description,
      params:doc.params,
      resps:doc.resps,
      httpMethod:doc.httpMethod,
      url:doc.url,
      group:doc.group
    }
    this.docModel.update(query,updater).exec(callback);
  }

  delete(docId:string,callback:Function)
  {
    this.docModel.remove({_id:docId}).exec(callback);
  }
}
